pp108 : Process Platform Translation Plug-in

Process Platform Translation Plug-in

The cordys.translation plug-in is used to translate messages and labels in the HTML pages. This plug-in works both on HTML5 pages on the server and in the Native App.

Methods

The following method is supported by the cordys.translation plug-in:

Table 1. List of Methods

Method

Description

getBundle(bundlePath, language)

Retrieves the javaScript message bundle specified by bundlePath in the specified language. If no language is provided, the current language of the user will be used by default. The bundle will be retrieved asynchronously and the method will return a deferred object on which you can call done() and fail() methods to respond on its state.

Message Bundle API

Once the message bundle is retrieved, you can call the methods on the bundle to translate texts to the language of the bundle. The following methods are available on a bundle:

Table 2. Methods on a bundle

Method

Description

getMessage(textIdentifier, ...)

Returns the translated text of the given textIdentifier. If the text contains insertions or arguments, they must be added for the respective insertions.

translate(selector, handler)

Translates HTML elements on the current page that match the given selector. If no selector is specified, all elements that have the attribute data-translatable set to true will be translated.
Optionally, a handler to a function can be specified, which will be called for each matching element. If no handler is specified, the translated text will display the value or text of the element. However, you must add the translated text to the object by using the script.

Examples

The following are the sample code snippets:

Retrieve Message Bundle
$.cordys.translation.getBundle("myBundle").done(function(mBundle) {
    var translatedMessage = mBundle.getMessage("Message to be translated");
});
Translate Label Elements
$.cordys.translation.getBundle("myBundle").done(function(mBundle) {
    mBundle.translate("LABEL");
});
Translate Message
$.cordys.translation.getBundle("myBundle").done(function(mBundle) {
    var translatedMessage = mBundle.getMessage("The capital of {0} is {1}", "UK", "London");
});
Translate Placeholder Attributes
$.cordys.translation.getBundle("myBundle").done(function(mBundle) {
    mBundle.translate("[placeholder]", function() {
        var $this = $(this);
        $this.attr("placeholder", mBundle.getMessage($this.attr("placeholder")));
        /* 
           eg. an input field like this:
               <input id="fldOrganization" placeholder="- Leave empty for default -" value=""/>
           will be translated for japanese to:
               <input id="fldOrganization" placeholder="- デフォルト�?�場�?��?�空�?��?��?��?� -" value=""/>
        */
    });
});